// ActionList.cpp : implementation file
//

#include "stdafx.h"
#include "CActionListDemo.h"
#include "ActionList.h"
#include "BtnST.h"
#include "btnst.cpp"


// CActionList

IMPLEMENT_DYNAMIC(CActionList, CListBox)

CActionList::CActionList()
{	

	//m_ButtonArray.SetSize((INT_PTR)10,(INT_PTR)1);
}

CActionList::~CActionList()
{
		for(int i=0; i<= m_ButtonArray.GetUpperBound();i++)
		delete m_ButtonArray.ElementAt(i);
}


BEGIN_MESSAGE_MAP(CActionList, CListBox)
END_MESSAGE_MAP()



// CActionList message handlers



void CActionList::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{

	// TODO:  Add your code to draw the specified item
	CDC dc;
	dc.Attach(lpDrawItemStruct->hDC);
	CRect ButtonRect = lpDrawItemStruct->rcItem;	//Gets the rect of the item
	
	CButtonST* Button = reinterpret_cast<CButtonST*>(lpDrawItemStruct->itemData);
	
	if(!Button)
		return;
	//ButtonRect.left+=50;

	Button->MoveWindow(ButtonRect);
	//dc.Rectangle(ButtonRect.left-50,ButtonRect.top,ButtonRect.left,ButtonRect.bottom);
	//pLedButton->CentreLED();
	dc.Detach();
	Button->ShowWindow( SW_SHOW );

}

void CActionList::AddItem()
{
	static int count = 0;
	CRect ButtonRect;
	CRect rect;
	count++;
	GetClientRect( rect );
	CButtonST *Button = new CButtonST;
	ButtonRect.SetRect( 50, 0, 0, 0 );
	Button->Create( NULL, WS_CHILD | WS_VISIBLE | WS_GROUP | WS_TABSTOP, ButtonRect, this, 1001+count );
	//NewButton->SetWindowPos(CWnd::FromHandle(HWND_TOP),rect.left,rect.top,rect.Width(),rect.Height(),SWP_NOSIZE);
	int AddedAt = m_ButtonArray.Add( Button );
	int nIndex = AddString( _T("") );
	Button->SetFlat(TRUE,TRUE);
	Button->SetBitmaps(IDB_BITMAP2,0,IDB_BITMAP1,1);
	Button->SetAlign(CButtonST::ST_ALIGN_OVERLAP,TRUE);
	Button->SizeToContent();
	Button->SetWindowTextW(_T("lol"));
	Button->DrawBorder(FALSE,TRUE);
	Button->SetFont(GetFont());
	Button->SetPressedStyle(CButtonST::BTNST_PRESSED_NONE,TRUE);
	Button->GetClientRect(rect);
	Button->GetClientRect(ButtonRect);
	//Button->ModifyStyle(0,
	//ButtonRect.left+=50;
	//Button->SetWindowPos(NULL, rect.left+10, rect.top, rect.Width(), rect.Height(), SWP_NOZORDER | SWP_NOSIZE);
	Button->MoveWindow(ButtonRect);
	SetWindowPos(NULL, 0, 0, rect.Width()+3, rect.Height()*MAX_NUMBER_OF_BUTTONS+4, SWP_NOZORDER | SWP_NOMOVE);
	CListBox::SetItemHeight( nIndex,rect.Height() );
	if(count>MAX_NUMBER_OF_BUTTONS) 
		SetWindowPos(NULL, 0, 0, rect.Width()+3+23, rect.Height()*MAX_NUMBER_OF_BUTTONS+4, SWP_NOZORDER | SWP_NOMOVE);
	if( nIndex!=LB_ERR && nIndex!=LB_ERRSPACE )
		SetItemData( nIndex, (DWORD)m_ButtonArray.ElementAt(AddedAt) );
	
	//Button->Invalidate();
	//pAddedButton = new CLedButton(ButtonText,this);
	//ButtonRect.SetRect( 0, 0, rect.Width(), (rect.Height() / NUMBER_OF_BUTTONS_IN_LIST_VIEW) );
	//pAddedButton->Create( "", WS_CHILD|BS_OWNERDRAW, ButtonRect, this, 1001+count );
	//
	//int AddedAt = m_ButtonArray.Add( pAddedButton );
	//
	////Adds a string and assigns nIndex the index of the current item
	//int nIndex = AddString( "" );

	//// Set a hight for the data item
	//CListBox::SetItemHeight( nIndex, rect.Height() / NUMBER_OF_BUTTONS_IN_LIST_VIEW );

	////If no error, associates the index with the button
	//if( nIndex!=LB_ERR && nIndex!=LB_ERRSPACE )
	//	SetItemData( nIndex, (DWORD)m_ButtonArray.ElementAt(AddedAt) );

}
BOOL CActionList::OnEraseBkgnd(CDC* pDC) 
{
	// Doing this speeds up drawing apparently
	return CWnd::OnEraseBkgnd(pDC);
	//return TRUE;
}